package com.sky.myapplication.java.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import com.sky.myapplication.R;
import com.sky.myapplication.java.observer.MainLifecycleObserver;

/**
 * Created by min
 * Time 2021/6/18
 * Desc
 */
public class AnotherActivity extends AppCompatActivity implements View.OnClickListener, LifecycleOwner {
    private String log = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);

        MainLifecycleObserver observer = new MainLifecycleObserver();
        getLifecycle().addObserver(observer);

        TextView tv_log = findViewById(R.id.tv_log);
        Button btn_back = findViewById(R.id.btn_back);
        Button btn_jump = findViewById(R.id.btn_jump);

        btn_back.setOnClickListener(this);
        btn_jump.setOnClickListener(this);
        //TextView 换行 \n
        log += "onCreate";
        tv_log.setText(log);

        Log.d("", "onCreate");
    }

    @Override
    public Lifecycle getLifecycle() {
        return super.getLifecycle();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_back:
                    finish();
                break;
            case R.id.btn_jump:

                //Intent启动activity四种方式
                //https://blog.csdn.net/daiyelang/article/details/8649134


                //1.显示启动  应用场景：常用于应用内部跳转
                //Intent intent = new Intent(AnotherActivity.this, ThreeActivity.class);

                //2.包名类名启动  应用场景：内部跳转+外部跳转
                Intent intent = new Intent();
                //intent.setClassName(AnotherActivity.this, "com.sky.myapplication.activity.ThreeActivity");

                //componentName跳转 应用场景：内部跳转＋外部跳转
                //intent.setComponent(new ComponentName(AnotherActivity.this, com.sky.myapplication.activity.ThreeActivity.class));


                //只通过category匹配是无法匹配到AActivity的。因为category属性是一个执行Action的附加信息。
                intent.setAction("ThreeActivity");
                //intent.setAction("android.intent.action.View");
                //intent.addCategory("aaa.bb.cc");
                startActivity(intent);

                break;
            default:
                break;
        }
    }

    //使用LifecycleObserver代替activity中的生命周期
//    @Override
//    protected void onResume() {
//        super.onResume();
//
//        log += "\n onResume";
//        tv_log.setText(log);
//        Log.d("", "onResume");
//    }
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//
//        log += "\n onPause";
//        tv_log.setText(log);
//        Log.d("", "onPause");
//    }
//
//    @Override
//    protected void onStart() {x
//        super.onStart();
//
//        log += "\n onStart";
//        tv_log.setText(log);
//        Log.d("", "onStart");
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//
//        log += "\n onStop";
//        tv_log.setText(log);
//        Log.d("", "onStop");
//    }
//
//    @Override
//    protected void onRestart() {
//        super.onRestart();
//
//        log += "\n onRestart";
//        tv_log.setText(log);
//        Log.d("", "onRestart");
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//
//        log += "\n onDestroy";
//        tv_log.setText(log);
//        Log.d("", "onDestroy");
//    }
}
